package com.ruoyi.framework.shiro.web.filter;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * App过滤器
 * 
 * @author ruoyi
 */
public class AppFilter implements Filter
{

    private ISysUserService userService;

    private FilterConfig config;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
       config = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse)servletResponse;
        String path = httpServletRequest.getRequestURI();
        String url = httpServletRequest.getRequestURL().toString();
        String host = url.replace(path, "");
        String staffId = httpServletRequest.getHeader("staffId");
        System.out.println("请求的接口：-------------------------------------" + path);
//        if (StringUtils.isNotEmpty(staffId)) {
//            if(!path.contains("/logout")) {
//                ServletContext sc = httpServletRequest.getSession().getServletContext();
//                WebApplicationContext cxt =WebApplicationContextUtils.getWebApplicationContext(sc);
//                if (cxt != null && cxt.getBean(ISysUserService.class) != null && userService == null) {
//                    userService = (ISysUserService) cxt.getBean(ISysUserService.class);
//                }
//                System.out.println(staffId);
//                SysUser user = userService.selectUserById(Long.valueOf(staffId));
//                if(user == null || "2".equals(user.getDelFlag())){
////                    httpServletResponse.sendRedirect(host+"/phone/warehouse/staff/logout?msg=用户不存在");
//                    httpServletRequest.getRequestDispatcher("/phone/warehouse/staff/logout?msg=用户不存在，请联系管理员").forward(servletRequest, servletResponse);
//                    return;
//                }
//                if("1".equals(user.getStatus())){
//                    httpServletRequest.getRequestDispatcher("/phone/warehouse/staff/logout?msg=用户被停用，请联系管理员").forward(servletRequest, servletResponse);
//                    return;
//                }
//            }
//        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
